<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/14">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell/11. case 选择/">case 选择</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 脚本中，除了用 if 来判断逻辑外，还有一种常用的方式，那就是 case 了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具体格式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">case</span> 变量 <span class="keyword">in</span></div><div class="line">value1)</div><div class="line">      <span class="built_in">command</span></div><div class="line">      ;;</div><div class="line">value2)</div><div class="line">      <span class="built_in">command</span></div><div class="line">      ;;</div><div class="line">value3)</div><div class="line">      <span class="built_in">command</span></div><div class="line">      ;;</div><div class="line">*)</div><div class="line">      Command</div><div class="line">      ;;</div><div class="line">Esac</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的结构中，不限制 value 的个数，* 代表了除了上面的 value 外的其他值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面来写一个判断输入数值是奇数或者偶数的脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim case.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Input a number:"</span> n</div><div class="line">a=$[<span class="variable">$n</span>%2]</div><div class="line"><span class="keyword">case</span> <span class="variable">$a</span> <span class="keyword">in</span></div><div class="line">    1)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"The number is odd."</span></div><div class="line">        ;;</div><div class="line">    0)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"The number is even."</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"It's not a number."</span></div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$a 的值为 1 或为 0 ，执行结果为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh case.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/case%20%E9%80%89%E6%8B%A9/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;case 脚本常用于编写系统服务的启动脚本。例如 /etc/init.d/iptables 中就用到了。另外有一个知识点，在给出的判断选项，即本例中的 0） 和 1） ，支持写成 1|0） ，意思是当变量 a 的值为 0 或 1 时，只不过在本例中这样的逻辑是不成立的，只是 case 判断脚本支持这样的写法。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell/1. shell 脚本介绍/">shell 脚本介绍</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Shell 脚本在 linux 系统管理员的运维工作中非常重要。它是一个脚本，并不能作为正式的编程语言。因为是跑在 linux 的shell 中，所以叫 shell 脚本。说白了， shell 脚本就是一些命令的合集。例如</p>
<ol>
<li>进入到 /tmp/ 目录；</li>
<li>列出当前目录中所有的文件名；</li>
<li>把所有当前的文件拷贝到 /root/ 目录下；</li>
<li>删除当前目录下所有文件。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;简单的四步在 shell 窗口中需要敲 4 个命令，按 4 次回车。当然这 4 步操作非常简单，如果是更加复杂的命令设置需要几十次操作，那样一次一次敲键盘会很麻烦。所以把所有的操作都记录到一个文档中，然后去调用文档中的命令，这样一步操作就可以完成。这个文档就是 shell 脚本，只是这个 shell 脚本有它的特殊格式。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本能让运维人员很方便的去管理服务器，因为可以指定一个任务计划定时去执行某一个 shell 脚本实现想要的需求。这对于 linux 系统管理员来说是一件非常值得自豪的事情。现在邮箱很好用，发邮件的同时还可以发一条邮件通知的信息给用户，利用这点，就可以在 linux 服务器上部署监控的 shell 脚本。比如，网卡流量有异常了或者服务器 web 服务器停止服务了，就可以发一封邮件给管理员，同时发送给管理员一个告警短信。这样就可以及时知道服务器出问题了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在正式写 shell 脚本之前，凡是自定义的脚本建议放到 /usr/local/sbin/ 目录下。这样做的目的是，一来可以更好的管理文档，二来为以后使用的人提供方便，知道自定义脚本放在哪里，方便维护。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/6. 让Samba支持软连接/">让Samba支持软连接</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;家里的系统由centos6换成linuxmint(实在是好用才换)了， 重新配置好samba后，发现里面的一个软链接到pt下载目录的目录不能访问。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试半天，chcon， chmod， chown，不行，仔细一想，应该是samba下软链接支持问题了，google后，解决，方案如下， 在smb.conf增加以下三行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">wide links = yes </div><div class="line">follow symlinks = yes </div><div class="line">unix extensions = no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启smbd服务即可。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/5. samba中文乱码的问题/">samba中文乱码的问题</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用SSH登陆远程的linux服务器，用win浏览运行samba的linux服务器中有中文名的文件夹的时候，看到里面的中文文件名都是乱码，而且想用shell来设置一下权限也设置不了。</p>
<ol>
<li>打开/etc/sysconfig/i18n</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">LANG=<span class="string">"zh_CN.GB2312"</span></div><div class="line">LANGUAGE=<span class="string">"zh_CN.GB18030:zh_CN.GB2312:zh_CN"</span></div><div class="line">SUPPORTED=<span class="string">"zh_CN.GB18030:zh_CN.GB2312:zh_CN.UTF-8:zh:en_US.UTF-8:en_US:en:ja_JP.UTF-8:ja_JP:ja"</span></div><div class="line">SYSFONT=<span class="string">"lat0-sun16"</span></div><div class="line">SYSFONTACM=<span class="string">"8859-15"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中LANG=”zh_CN.GB2312” 是必须的(如果不想让中文乱码的话!!!)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其它的可以按照自已的需求来改变。</p>
<ol>
<li>打开smb.conf</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">display charset=cp936</div><div class="line"> unix charset=cp936</div><div class="line"> dos  charset=cp936</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重新启动系统即可。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/4. samba同时用匿名和用户登录/">samba同时用匿名和用户登录</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于同时使用匿名和用户登录，配置文件如下，已经验证成功：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line">        workgroup = WORKGROUP</div><div class="line">        server string = Samba Server Version %v</div><div class="line">        <span class="built_in">log</span> file = /var/<span class="built_in">log</span>/samba/<span class="built_in">log</span>.%m</div><div class="line">        max <span class="built_in">log</span> size = 50</div><div class="line">        security = user</div><div class="line">        passdb backend = tdbsam</div><div class="line">        load printers = yes</div><div class="line">        cups iptions = raw</div><div class="line">        map to guest = bad user</div><div class="line">        guest account = nobody</div><div class="line">        encrypt password = yes</div><div class="line">        smb passwd file = /etc/samba/smbpasswd</div><div class="line"></div><div class="line">[public]</div><div class="line">        comment = public</div><div class="line">        path = /data/pub/public</div><div class="line">        browseable = yes</div><div class="line">        guest ok = yes</div><div class="line">        writable = yes</div><div class="line">        printable = no</div><div class="line">        create mask = 0644</div><div class="line">        directory mask = 0755</div><div class="line"></div><div class="line">[project]</div><div class="line">        comment = project</div><div class="line">        path = /data/pub/project</div><div class="line">        browseable = yes</div><div class="line">        public = no</div><div class="line">        guest ok = no</div><div class="line">        writeable = yes</div><div class="line">        force user = root</div><div class="line">        printable = no</div><div class="line">        create mask = 0644</div><div class="line">        directory mask = 0755</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：需要手动创建 /data/pub/public 和 /data/pub/project 目录，为了顺利完成试验，需要提前先改成 777 权限。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外还需创建用户：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># useradd testuser</span></div><div class="line">[root@192 ~]<span class="comment"># pdbedit -a testuser</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/3. samba 用户密码的几种方式对比/">samba 用户密码的几种方式对比</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;passdb backend就是用户后台的意思。目前有三种后台：smbpasswd、tdbsam和ldapsam。sam应该是security account manager（安全账户管理）的简写。</p>
<h2 id="1-smbpasswd"><a href="#1-smbpasswd" class="headerlink" title="1.smbpasswd"></a>1.smbpasswd</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">passdb backend = smbpasswd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该方式是使用smb自己的工具smbpasswd来给系统用户（真实用户或者虚拟用户）设置一个Samba密码，客户端就用这个密码来访问Samba的资源。smbpasswd文件默认在/etc/samba目录下，不过有时候要手工建立该文件。</p>
<ul>
<li>smbpasswd -a 用户名 #添加一个samba用户</li>
<li>smbpasswd -d 用户名 #禁用一个samba用户</li>
<li>smbpasswd -e 用户名 #恢复一个samba用户</li>
<li>smbpasswd -x 用户名 #删除一个samba用户</li>
</ul>
<h2 id="2-tdbsam"><a href="#2-tdbsam" class="headerlink" title="2.tdbsam"></a>2.tdbsam</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">passdb backend = tdbsam</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该方式则是使用一个数据库文件来建立用户数据库。数据库文件叫passdb.tdb，默认在/etc/samba目录下。passdb.tdb 用户数据库可以使用smbpasswd –a来建立Samba用户，不过要建立的Samba用户必须先是系统用户。我们也可以使用pdbedit命令来建立Samba账户并由其pdbedit管 理。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户的建立可以先用mksmbpasswd建立一个smppasswd文件，然后用pdbedit将文件里的用户导入数据库。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat /etc/passwd | mksmbpasswd &gt; /etc/samba/smbpasswd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pdbedit命令的参数很多，我们列出几个主要的：</p>
<ul>
<li>pdbedit -i smbpasswd:/etc/samba/smbpasswd</li>
<li>pdbedit -a username：新建Samba账户。</li>
<li>pdbedit -x username：删除Samba账户。</li>
<li>pdbedit -L：列出Samba用户列表，读取passdb.tdb数据库文件。</li>
<li>pdbedit -Lv：列出Samba用户列表的详细信息。</li>
<li>pdbedit -c “[D]” –u username：暂停该Samba用户的账号。</li>
<li>pdbedit -c “[]” –u username：恢复该Samba用户的账号。</li>
</ul>
<h2 id="3-ldapsam"><a href="#3-ldapsam" class="headerlink" title="3.ldapsam"></a>3.ldapsam</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">passdb backend = smbpasswd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该方式则是基于LDAP的账户管理方式来验证用户。首先要建立LDAP服务，然后设置“passdb backend = ldapsam:ldap://LDAP Server”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：samba3.x的早期版本默认使用tdb库也就是smb.conf默认设定为passdb backend = tdbsam，只需要注释掉该行添加smb passwd file = /etec/samba/smbpasswd即可使用smbpasswd存储加密密钥。samba3.5.6更加规范了passdb backend参数的使用，取消了smb passwd file设定，如果简单注释掉passdb backend参数，密钥文件也不会被存储到smbpasswd。所以很多人说怎么修改smb.conf的配置，</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里只能说samba版本升级了，规则也变化了。现在无论是使用tdb数据库存储密钥还是smbpasswd文本存储密钥都要设定相对应的 passdb backend参数。要用传统的文本方式存储只需这样设定passdb backend = smbpasswd:/etc/samba/smbpasswd（后面跟的是绝对路径）&gt;，不要再画蛇添足的写上smb passwd file = /etec/samba/smbpasswd，该参数已经不适用于新版本的samba了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，这里的用户名必须是linux中存在的用户，可以使用useradd命令在系统中添加一个用户，然后再增加一个对应的samba用户，也 就是一个用户名使用的是两套密码。一个是系统用户密码，另一个密码存储在/etc/samba/smbpasswd文件中的samba密码，这样可以防止 系统用户密钥外泄带来的安全隐患。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除了上面的措施外，samba还提供了一个更安全的方法，用户名映射功能，这样做的好处是防止系统内的真实用户名暴露，在smb.conf中增 加username map = /etc/samba/smbuser设定，再手工建立该文件。username map参数详解，比如有一个系统用户名为zyhyt.org，同时我们也设定其为samba的登录名，虽然是两套独立的密码，但依然告诉了用户，我系统内 也存在zyhyt.org这个用户。严格的说这也是违背系统安全规则的，不法人士可能会利用该用户名暴力猜解获得系统内帐户权限。samba提供的用户名 映射功能，只需编辑smbuser文&gt;件，格式为：真实的用户名 = 映射出的用户名（随便自定义）；zyhyt.org = nas_guest nas_nobody（可以映射出多个用户名，注意中间的空格）。设定完成后，我们只需将nas_guest告诉用户即可，无须担心真实的 zyhyt.org用户名暴露。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/1. samba部署和优化/">samba部署和优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;samba 服务可以实现 windows 和 linux 的文件共享，配置不难，使用也非常简单。</p>
<h2 id="1-samba-配置文件-smb-conf"><a href="#1-samba-配置文件-smb-conf" class="headerlink" title="1.samba 配置文件 smb.conf"></a>1.samba 配置文件 smb.conf</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装系统的时候大多会默认安装 samba ，如果没有安装，在 centos 上只需要运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y samba samba-client</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;samba 配置文件为 /etc/samba/smb.conf ，通过修改这个配置文件来完成各种需求。打开配置文件，发现很多内容都用 # 或者 ； 注释掉了，先看一下未被注释的部分</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line">    workgroup = MYGROUP</div><div class="line">    server string = Samba Server Version %v</div><div class="line">    security = user</div><div class="line">    passdb backend = tdbsam</div><div class="line">    load printers = yes</div><div class="line">    cups options = raw</div><div class="line">[homes]</div><div class="line">    comment = Home Directories</div><div class="line">    browseable = no</div><div class="line">    writable = yes</div><div class="line">[printers]</div><div class="line">    comment = All Printers</div><div class="line">    path = /var/spool/samba</div><div class="line">    browseable = no</div><div class="line">    guest ok = no</div><div class="line">    writable = no</div><div class="line">    printable = yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主要有以上三个部分：<strong>[global] , [homes] , [printers]</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>[global]</strong> 定义全局的配置， workgroup 用来定义工作组，相信如果安装过 windows 系统，都会对这个 workgroup 不陌生。一般情况下，需要把这里的 MYGROUP 改成 WORKGROUP （windows 默认的工作组名字）。</p>
<ul>
<li><strong>security = user</strong> ：这里指定 samba 的安全等级。关于安全等级有四种：</li>
<li><p><strong>share</strong> ：用户不需要帐号密码即可登录 samba 服务器</p>
</li>
<li><p><strong>user</strong> ：由提供服务的 samba 服务器负责检查账户及密码（默认）</p>
</li>
<li><p><strong>server</strong> ：检查账户及密码的工作由另一台 windows 或 samba 服务器负责</p>
</li>
<li><p><strong>domain</strong> ：指定 windows 域控制服务器来验证用户的账户及密码</p>
</li>
<li><p><strong>passdb bankend = tdbsam</strong> ：passdb backend （用户后台），samba有三种用户后台：<strong>smbpasswd，tdbsam 和 ldapsam</strong>。</p>
</li>
<li><p><strong>smbpasswd</strong> ：该方式是使用 smb 工具 smbpasswd 给系统用户（真实用户或者虚拟用户）设置一个 samba 密码，客户端就用此密码访问 samba 资源。smbpasswd 在 /etc/samba 中，有时需要手工创建该文件。</p>
</li>
<li><p><strong>tdbsam</strong>：使用数据库文件创建用户数据库。数据库文件叫 passdb.tdb，在 /etc/samba 中。passdb.tdb 用户数据库可使用 smbpasswd -a 创建 samba 用户，要创建的 samba 用户必须先是系统用户。也可以使用 pdbedit 创建 samba 账户。pdbedit 参数很多，其中主要的有：</p>
</li>
<li><p><strong>pdbedt -a username</strong> ：新建 samba 账户</p>
</li>
<li><p><strong>pdbedit -x username</strong> ：删除 samba 账户</p>
</li>
<li><p><strong>pdbedit -L</strong> ：列出 samba 用户列表，读取 passdb.tdb 数据库文件</p>
</li>
<li><p><strong>pdbedit -Lv</strong> ：列出 samba 用户列表详细信息</p>
</li>
<li><p><strong>pdbedit -c “[D]” -u username</strong> ：暂停该 samba 用户帐号</p>
</li>
<li><p><strong>pdbeidt -c “[]” -u username</strong> ：恢复该 samba 用户帐号</p>
</li>
<li><p><strong>ldapsam</strong> ：基于 LDAP  账户管理方式验证用户。首先要建立 LDAP 服务，设置 “passdb backend = ldapsam：ldap://LDAP Server”</p>
</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;load printers 和 cups options 两个参数用来设置打印机相关</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除了这些参数外，还有几个参数需要了解：</p>
<ul>
<li><p><strong>netbios name = MYSERVER</strong> ：设置出现在网上邻居中的主机名</p>
</li>
<li><p><strong>hosts allow = 127.192.168.12.192.168.13</strong> ：用来设置允许的主机，如果在前面加 “；” 则表示允许所有主机。</p>
</li>
<li><p><strong>log file =  /var/log/samba/%m.log</strong> ：定义 samba 的日志，这里的 %m 是上面的 netbios name</p>
</li>
<li><p><strong>max log size = 50</strong> ：指定日志的最大容量，单位是K</p>
</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[home] 该部分内容共享用户子机的家目录，也就是说，当用户登录到 samba 服务器上时，实际上是进入到了该用户的家目录，用户登录后，共享名不是 homes 而是用户子机的标识符，对于单纯的文件共享的环境来说这部分可以注释掉。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[printers] 该部分内容设置打印机共享</p>
<h2 id="2-samba实践"><a href="#2-samba实践" class="headerlink" title="2.samba实践"></a>2.samba实践</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;samba 可以实现 linux 和 windows 机器相互共享文件，非常实用。下面实践几个应用。</p>
<h3 id="A-samba-实践一"><a href="#A-samba-实践一" class="headerlink" title="A.samba 实践一"></a>A.samba 实践一</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要求：共享一个目录，任何人都可以访问，即不用输入密码即可访问，要求制度</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打开 samba 配置文件 /etc/samba/smb.conf</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># vim /etc/samba/smb.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 [global] 部分把：MYGROUP 改成 WORKGROUP ，把 security = user 修改为 security = share 。然后在文件的最末尾处加入以下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[share]</div><div class="line">comment = share all</div><div class="line">path = /tmp/samba</div><div class="line">browseable = yes</div><div class="line">public = yes</div><div class="line">writable = no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建测试目录：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /tmp/samba</span></div><div class="line">[root@192 ~]<span class="comment"># chmod 777 /tmp/samba</span></div><div class="line">[root@192 ~]<span class="comment"># touch /tmp/samba/sharefiles</span></div><div class="line">[root@192 ~]<span class="comment"># echo "111111" &gt; /tmp/samba/sharefiles</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动 samba 服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># /etc/init.d/smb start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试能否试验要求，首先测试配置的 smb.conf 是否正确，使用命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># testparm</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;会看到一个警告：<code>WARNING: The security=share option is deprecated</code> ，不过影响不大，无需管它。如果没有错就在 windows 机器上的浏览器中输入：file://192.168.0.73/sharefiles 看能否访问到</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 linux 机器上输入：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># smbclient //192.168.0.73/share</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有个方法就是挂载，在挂载前先安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># yum install -y cifs-utils</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;挂载</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mount -t cifs //192.168.0.73/share /opt/</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/samba%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/01.png?raw=true" alt=""></figure></p>
<h3 id="B-samba-实践二"><a href="#B-samba-实践二" class="headerlink" title="B.samba 实践二"></a>B.samba 实践二</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要求：共享一个目录，使用用户名和密码登录后才可以访问，要求可以读写：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打开 samba 的配置文件 /etc/samba/smb.conf</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># vim /etc/samba/smb.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[global] 部分内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line">        workgroup = WORKGROUP</div><div class="line">        server string = Samba Server Version %v</div><div class="line">        security = user</div><div class="line">        passdb backend = tdbsam</div><div class="line">        load printers = yes</div><div class="line">        cups options = raw</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还需要加入一下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[myshare]</div><div class="line">        comment = share <span class="keyword">for</span> users</div><div class="line">        path = /samba</div><div class="line">        browseable = yes</div><div class="line">        writable = yes</div><div class="line">        public = no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存配置文件，创建目录：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mkdir /samba</span></div><div class="line">[root@192 ~]<span class="comment"># chmod 777 /samba</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后添加用户。因为在 [global] 中 “passdb backend = tdbsam”，所以要使用 pdbedit 来增加用户，注意添加的用户必须在系统中存在，所以需要先创建系统帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># useradd user1</span></div><div class="line">[root@192 ~]<span class="comment"># useradd user2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后添加 user1 为 samba 帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pdbedit -a user1</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在添加 user2 为 samba 帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pdbedit -a user2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列出 samba 的所有帐号：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># pdbedit -L</span></div><div class="line">user1:503:</div><div class="line">user2:504:</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 samba 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># service smb restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打开浏览器输入 file://192.168.0.73/myshare 弹出窗口后输入帐号和密码登录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux 机器登录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具体语法为：smbclient //IP/共享名 -U 用户名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># smbclient //192.168.0.73/myshare -U user1</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/samba%E9%83%A8%E7%BD%B2%E5%92%8C%E4%BC%98%E5%8C%96/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以用 “？” 列出所有可以使用的命令。常用的有 cd，ls，rm，pwd，tar ，mkdir，chown，get，put等，使用 help+ 命令 可以打印该命令如何使用，其中 get 是下载，put 是上传。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外还可以通过挂载方式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># mount -t cifs //192.168.0.73/myshare /mnt -o username=user2,password=123456</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要指定 -t cifs //IP/共享名 本地挂载点 -o后面跟 username 和 password 挂载完后就可以像使用本地的目录一样使用共享目录了，注意共享名后面不能有斜杠。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Samba/2. Samba服务器（多用户组、多用户有不同的访问权限）/">Samba服务器（多用户组、多用户有不同的访问权限）</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Samba/">Samba</a>
			</span>
		
	</div>

	

	
		<ol>
<li><p>首先服务器采用用户验证的方式，每个用户可以访问自己的宿主目录，并且只有该用户能访问宿主目录，并具有完全的权限，而其他人不能看到你的宿主目录。</p>
</li>
<li><p>建立一个caiwu的文件夹，希望caiwu组和lingdao组的人能看到，network02也可以访问，但只有caiwu01有写的权限。</p>
</li>
<li><p>建立一个lindao的目录，只有领导组的人可以访问并读写，还有network02也可以访问，但外人看不到那个目录</p>
</li>
<li><p>建立一个文件交换目录exchange，所有人都能读写，包括guest用户，但每个人不能删除别人的文件。</p>
</li>
<li><p>建立一个公共的只读文件夹public，所有人只读这个文件夹的内容。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;前期的工作</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;建立3个组：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">groupadd caiwu</div><div class="line"></div><div class="line">groupadd network</div><div class="line"></div><div class="line">groupadd lingdao</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加用户并加入相关的组当中：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">useradd caiwu01 -g caiwu</div><div class="line"></div><div class="line">useradd caiwu02 -g caiwu</div><div class="line"></div><div class="line">useradd network01 -g network</div><div class="line"></div><div class="line">useradd network02 -g network</div><div class="line"></div><div class="line">useradd lingdao01 -g lingdao</div><div class="line"></div><div class="line">useradd lingdao02 -g lingdao</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后我们使用smbpasswd -a caiwu01的命令为6个帐户分别添加到samba用户中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">mkdir /home/samba</div><div class="line"></div><div class="line">mkdir /home/samba/caiwu</div><div class="line"></div><div class="line">mkdir /home/samba/lingdao</div><div class="line"></div><div class="line">mkdir /home/samba/exchange</div><div class="line"></div><div class="line">mkdir /home/samba/public</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了避免麻烦可以在这里把上面所有的文件夹的权限都设置成777，通过samba灵活的权限管理来设置上面的5点要求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是smb.conf的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line"></div><div class="line">workgroup = bmit </div><div class="line"></div><div class="line"><span class="comment">#我的网络工作组</span></div><div class="line"></div><div class="line">server string = Frank<span class="string">'s Samba File Server</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#我的服务器名描述</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">security = user</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#使用用户验证机制</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">encrypt passwords = yes</span></div><div class="line"><span class="string">smb passwd file = /etc/samba/smbpasswd</span></div><div class="line"><span class="string">#使用加密密码机制，在win95和winnt使用的是明文</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">其他的基本上可以按照默认的来。</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">[homes]</span></div><div class="line"><span class="string">comment = Home Directories</span></div><div class="line"><span class="string">browseable = no</span></div><div class="line"><span class="string">writable = yes</span></div><div class="line"><span class="string">valid users = %S</span></div><div class="line"><span class="string">create mode = 0664</span></div><div class="line"><span class="string">directory mode = 0775</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#homes段满足第1条件</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">[caiwu]</span></div><div class="line"><span class="string">comment = caiwu</span></div><div class="line"><span class="string">path = /home/samba/caiwu</span></div><div class="line"><span class="string">public = no</span></div><div class="line"><span class="string">valid users = @caiwu,@lingdao,network02</span></div><div class="line"><span class="string">write list = caiwu01</span></div><div class="line"><span class="string">printable = no</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#caiwu段满足我们的第2要求</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">[lingdao]</span></div><div class="line"><span class="string">comment = lingdao</span></div><div class="line"><span class="string">path = /home/samba/lingdao</span></div><div class="line"><span class="string">public = no</span></div><div class="line"><span class="string">browseable = no</span></div><div class="line"><span class="string">valid users = @lingdao,network02</span></div><div class="line"><span class="string">printable = no</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#lingdao段能满足我们的第3要求</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">[exchage]</span></div><div class="line"><span class="string">comment = Exchange File Directory</span></div><div class="line"><span class="string">path = /home/samba/exchange</span></div><div class="line"><span class="string">public = yes</span></div><div class="line"><span class="string">writable = yes</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">#exchange段基本能满足我们的第4要求，但不能满足每个人不能删除别人的文件这个条件，即使里设置了mask也是没用，其实这个条件只要unix设置一个粘着位就行</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chmod -R 1777 /home/samba/exchange</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意这里权限是1777，类似的系统目录/tmp也具有相同的权限，这个权限能实现每个人能自由写文件，但不能删除别人的文件这个要求</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[public]</div><div class="line">comment = Read Only Public</div><div class="line">path = /home/samba/public</div><div class="line">public = yes</div><div class="line"><span class="built_in">read</span> only = yes</div><div class="line"></div><div class="line"><span class="comment">#这个public段能满足第5要求。</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到此为止设置已经能实现共享文件要求，记得重启服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#/etc/rc.d/init.d/smb restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果大家没有winodws，不妨先用samba的cilent端命令来测试一下</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;命令的用法举几个例子</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">smbclient -L 服务器ip -N</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;guest帐户查询服务器的samba共享情况，可以检验一下是否lingdao目录时候能被guest帐户看到，应该是看不到的，当然也可以以某个用户的名义查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">smbclient -L 服务器ip -U caiwu01</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;系统会提示密码，只要输入smb密码就行。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">smbclient //服务器ip/caiwu -U caiwu01</div><div class="line"></div><div class="line"><span class="comment">#以caiwu01用户的名义登录caiwu目录</span></div><div class="line"></div><div class="line">smbmount //服务器ip/caiwu /mnt/caiwu -o username=caiwu01</div><div class="line"></div><div class="line"><span class="comment">#把服务器的财务目录映射到本地的/mnt/caiwu目录。</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">smbclient -L //localhost/share</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者　</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">smbclient-L \\127.0.0.1 -Umyname</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时输入的密码就是你刚才设置的samba密码使用</p>
<ol>
<li>windows用户</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在我的电脑地址栏里输入\192.168.1.1访问；也可windows+R输入\192.168.1.1；<br>登录后可以右击映射到本地驱动器。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">net use * /delete</div></pre></td></tr></table></figure>
<ol>
<li>Linux</li>
</ol>
<ul>
<li>使用smbclient</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#smbclient//192.168.1.1/Normal -U user%passwd</span></div></pre></td></tr></table></figure>
<ul>
<li>挂载到某个目录使用</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#mkdir/mnt/share</span></div><div class="line"><span class="comment">#mount -o username=youruser,password=passwd //192.168.1.1/Normal  /mnt/share</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置开机挂载将如下命令写入/etc/fstab</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">//192.168.1.1/share  /mnt/ml45  cifs  defaults,auto,username=youruser,password=passwd 0 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后#mount -a</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Saltstack/1. Saltstack 安装配置/">resin安装与配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Saltstack/">Saltstack</a>
			</span>
		
	</div>

	

	
		<h2 id="1-saltstack安装"><a href="#1-saltstack安装" class="headerlink" title="1.saltstack安装"></a>1.saltstack安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;准备两台机器 192.168.0.84  192.168.0.83</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置hostname</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.83</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置hosts </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /etc/hosts</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.83</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># vim /etc/hosts</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭selinux，清空iptables规则</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@web9 ~]<span class="comment"># yum install -y salt-master salt-minion</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.83：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@web10 ~]<span class="comment"># yum install -y salt-minion</span></div></pre></td></tr></table></figure>
<h3 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84上：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件 /etc/salt/minion </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /etc/salt/minion</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大概16行修改或增加    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">master: 192.168.0.84</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># service salt-master start</span></div><div class="line">Starting salt-master daemon: [确定]</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># service salt-minion start</span></div><div class="line">Starting salt-minion daemon: [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.83上：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件 /etc/salt/minion </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># vim /etc/salt/minion</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;大概16行修改或增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">master: 192.168.0.84</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># service salt-minion start</span></div><div class="line">Starting salt-minion daemon: [确定]</div></pre></td></tr></table></figure>
<h3 id="配置认证"><a href="#配置认证" class="headerlink" title="配置认证"></a>配置认证</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下操作都是在192.168.0.84上：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在要把 web10 加到 Accepted Keys</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt-key -a web10.yanyi.com</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时我们在client的 /etc/salt/pki/minion 目录下面会多出一个minion_master.pub 文件</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用 salt-key 命令查看到已经签名的客户端</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt-key 可以使用-A签名所有主机，也可以使用-d删除指定主机的key，-D删除所有主机。</p>
<h3 id="远程执行命令"><a href="#远程执行命令" class="headerlink" title="远程执行命令"></a>远程执行命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;示例1： salt ‘<em>‘ test.ping    这里的</em>表示所以已经签名的客户端，也可以指定其中一个，比如web10.yanyi.com</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;示例2:  salt ‘*’ cmd.run   ‘df -h’</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： 这里的<em>必须是在master上已经被接受过的客户端，可以通过salt-key查到，通常是我们已经设定的id值。关于这部分内容，它支持通配、列表以及正则。 比如两台客户端  web10  web11， 那我们可以写成  salt ‘web</em>’    salt ‘web1[01]’   salt -L ‘web10,web11’   salt -E ‘web(10|11) 等形式，使用列表，即多个机器用 逗号分隔，而且需要加-L，使用正则必须要带-E选项&gt;。 它还支持grains，加-G选项，     pillar 加-I选项，下面会介绍到。</p>
<h2 id="2-saltstack-grains"><a href="#2-saltstack-grains" class="headerlink" title="2.saltstack grains"></a>2.saltstack grains</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;grains是在minion启动时收集到的一些信息，比如操作系统类型、网卡ip、内核版本、cpu架构等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt ‘web10.yanyi.com’ grains.ls 列出所有的grains项目名字</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' grains.ls</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt ‘web10.yanyi.com’ grains.items 列出所有grains项目以及值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' grains.items</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;grains的信息并不是动态的，并不会时时变更，它只是在minion启动时收集到的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们可以根据grains收集到的一些信息，做配置管理工作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;grains支持自定义信息。</p>
<h3 id="自定义-grains"><a href="#自定义-grains" class="headerlink" title="自定义 grains"></a>自定义 grains</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.83上，编辑文件 /etc/salt/grains </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># vim /etc/salt/grains</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">role: nginx</div><div class="line">env: <span class="built_in">test</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启minion服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@web10 ~]<span class="comment"># service salt-minion restart</span></div><div class="line">Stopping salt-minion daemon:               [确定]</div><div class="line">Starting salt-minion daemon:               [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84上：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;获取 grains：salt ‘*’ grains.item role env</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt '*' grains.item role env</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以借助grains的一些属性信息来执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt -G role:nginx cmd.run 'hostname'</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<h2 id="3-saltstack-pillar"><a href="#3-saltstack-pillar" class="headerlink" title="3.saltstack pillar"></a>3.saltstack pillar</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pillar和grains不一样，是在master上定义的，并且是针对minion定义的一些信息。像一些比较重要的数据（密码）可以存在pillar里，还可以定义变量等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置自定义pillar  /etc/salt/master</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /etc/salt/master</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到如下配置：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;去掉前面的警号</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/18.png?raw=true" alt="18"><figcaption class="figure__caption">18</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建此目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># mkdir /srv/pillar</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /srv/pillar/test.sls </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/pillar/test.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">conf: /etc/123.conf</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/19.png?raw=true" alt="19"><figcaption class="figure__caption">19</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /srv/pillar/top.sls  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/pillar/top.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">base:</div><div class="line">  <span class="string">'web10.yanyi.com'</span>:</div><div class="line">    - <span class="built_in">test</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/20.png?raw=true" alt="20"><figcaption class="figure__caption">20</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启master</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># service salt-master restart</span></div><div class="line">Stopping salt-master daemon: [确定]</div><div class="line">Starting salt-master daemon: [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当更改完pillar配置文件后，我们可以通过刷新pillar配置来获取新的pillar状态：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt ‘*’ saltutil.refresh_pillar</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/21.png?raw=true" alt="21"><figcaption class="figure__caption">21</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;验证：salt  ‘*’ pillar.item conf</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/22.png?raw=true" alt="22"><figcaption class="figure__caption">22</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pillar同样可以用来作为salt的匹配对象。比如 salt  -I ‘conf:/etc/123.conf’  test.ping</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/23.png?raw=true" alt="23"><figcaption class="figure__caption">23</figcaption></figure></p>
<h2 id="4-配置管理安装-apache"><a href="#4-配置管理安装-apache" class="headerlink" title="4.配置管理安装 apache"></a>4.配置管理安装 apache</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.84上，编辑配置文件 /etc/salt/master</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /etc/salt/master</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;搜索找到 file_roots</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/24.png?raw=true" alt="24"><figcaption class="figure__caption">24</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打开如下内容的注释：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/25.png?raw=true" alt="25"><figcaption class="figure__caption">25</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建 /stv/salt 目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># mkdir /srv/salt</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /srv/salt/top.sls </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入如下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">base:</div><div class="line">  <span class="string">'*'</span>:</div><div class="line">    - apache</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/26.png?raw=true" alt="26"><figcaption class="figure__caption">26</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;意思是，在所有的客户端上执行 apache模块</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启 /etc/init.d/salt-master restart</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># service salt-master restart </span></div><div class="line">Stopping salt-master daemon: [确定]</div><div class="line">Starting salt-master daemon: [确定]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 /srv/salt/apache.sls </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/apache.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入如下内容，这个就是apache模块的内容</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里如果只有一个服务，那么就可以写成 –name: httpd 不用再换一行了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">apache-service:</div><div class="line">  pkg.installed:</div><div class="line">    - names: </div><div class="line">      - httpd</div><div class="line">      - httpd-devel</div><div class="line">  service.running:</div><div class="line">    - name: httpd</div><div class="line">    - <span class="built_in">enable</span>: True</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/27.png?raw=true" alt="27"><figcaption class="figure__caption">27</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明，apache-service是id的名字，自定义的。pkg.installed 为包安装函数，下面是要安装的包的名字。service.running也是一个函数，来保证指定的服务启动，enable表示开机启动。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行： salt ‘web10.yanyi.com’ state.highstate</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/28.png?raw=true" alt="28"><figcaption class="figure__caption">28</figcaption></figure></p>
<h2 id="5-配置文件和目录管理"><a href="#5-配置文件和目录管理" class="headerlink" title="5.配置文件和目录管理"></a>5.配置文件和目录管理</h2><h3 id="配置管理文件"><a href="#配置管理文件" class="headerlink" title="配置管理文件"></a>配置管理文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/filetest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">file_test:</div><div class="line">  file.managed:</div><div class="line">    - name: /tmp/yanyi.com</div><div class="line">    - <span class="built_in">source</span>: salt://<span class="built_in">test</span>/123/1.txt</div><div class="line">    - user: root</div><div class="line">    - group: root</div><div class="line">    - mode: 644</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/29.png?raw=true" alt="29"><figcaption class="figure__caption">29</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：第一行的file_test为自定的名字，表示该配置段的名字，可以在别的配置段中引用它，source指定文件从哪里拷贝，这里的test相当于是/srv/salt/test</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># mkdir /srv/salt/test</span></div><div class="line">[root@web9 ~]<span class="comment"># mkdir /srv/salt/test/123</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改 top.sls </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/30.png?raw=true" alt="30"><figcaption class="figure__caption">30</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/31.png?raw=true" alt="31"><figcaption class="figure__caption">31</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/32.png?raw=true" alt="32"><figcaption class="figure__caption">32</figcaption></figure></p>
<h3 id="配置管理目录"><a href="#配置管理目录" class="headerlink" title="配置管理目录"></a>配置管理目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/dirtest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">file_dir:</div><div class="line">  file.recurse:</div><div class="line">    - name: /tmp/testdir</div><div class="line">    - <span class="built_in">source</span>: salt://<span class="built_in">test</span>/123</div><div class="line">    - user: root</div><div class="line">    - file_mode: 644</div><div class="line">    - dir_mode: 755</div><div class="line">    - mkdir: True</div><div class="line">    - clean: True</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/33.png?raw=true" alt="33"><figcaption class="figure__caption">33</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>clean: True</strong> 加上它之后，源删除文件或目录，目标也会跟着删除，否则不会删除</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考  <a href="http://www.0550go.com/automation-deployment/saltstack/saltstack-file-recurse.html" target="_blank" rel="external">http://www.0550go.com/automation-deployment/saltstack/saltstack-file-recurse.html</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 top.sls</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/34.png?raw=true" alt="34"><figcaption class="figure__caption">34</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/35.png?raw=true" alt="35"><figcaption class="figure__caption">35</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看客户端</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/36.png?raw=true" alt="36"><figcaption class="figure__caption">36</figcaption></figure></p>
<h2 id="6-配置管理远程命令"><a href="#6-配置管理远程命令" class="headerlink" title="6.配置管理远程命令"></a>6.配置管理远程命令</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/cmdtest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">cmd_test:</div><div class="line">  cmd.run:</div><div class="line">    - onlyif: <span class="built_in">test</span> -f /tmp/111.txt</div><div class="line">    - names:</div><div class="line">      - touch /tmp/111.txt</div><div class="line">      - mkdir /tmp/1233</div><div class="line">    - user: root</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/37.png?raw=true" alt="37"><figcaption class="figure__caption">37</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以加unless，和onlyif正好相反</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 top.sls</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/38.png?raw=true" alt="38"><figcaption class="figure__caption">38</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/39.png?raw=true" alt="39"><figcaption class="figure__caption">39</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看客户端</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/40.png?raw=true" alt="40"><figcaption class="figure__caption">40</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重新修改配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/cmdtest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">cmd_test:</div><div class="line">  cmd.run:</div><div class="line">    - unless: <span class="built_in">test</span> -f /tmp/111.txt</div><div class="line">    - names:</div><div class="line">      - touch /tmp/111.txt</div><div class="line">      - mkdir /tmp/1233</div><div class="line">    - user: root</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/41.png?raw=true" alt="41"><figcaption class="figure__caption">41</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/42.png?raw=true" alt="42"><figcaption class="figure__caption">42</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看客户端</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/43.png?raw=true" alt="43"><figcaption class="figure__caption">43</figcaption></figure></p>
<h2 id="7-配置管理远程脚本"><a href="#7-配置管理远程脚本" class="headerlink" title="7.配置管理远程脚本"></a>7.配置管理远程脚本</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/shelltest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">shell_test:</div><div class="line">  cmd.script:</div><div class="line">    - <span class="built_in">source</span>: salt://<span class="built_in">test</span>/1.sh</div><div class="line">    - user: root</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/44.png?raw=true" alt="44"><figcaption class="figure__caption">44</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑脚本文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/test/1.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">touch /tmp/111.txt</div><div class="line"><span class="keyword">if</span> [ -d /tmp/1233 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    rm -rf /tmp/1233</div><div class="line"><span class="keyword">else</span></div><div class="line">    mkdir /tmp/1233</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/45.png?raw=true" alt="45"><figcaption class="figure__caption">45</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 top.sls</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/46.png?raw=true" alt="46"><figcaption class="figure__caption">46</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/47.png?raw=true" alt="47"><figcaption class="figure__caption">47</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/48.png?raw=true" alt="48"><figcaption class="figure__caption">48</figcaption></figure></p>
<h2 id="8-配置管理任务计划"><a href="#8-配置管理任务计划" class="headerlink" title="8.配置管理任务计划"></a>8.配置管理任务计划</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/crontest.sls</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">cron_test:</div><div class="line">  cron.present:</div><div class="line">    - name: /bin/touch /tmp/111.txt</div><div class="line">    - user: root</div><div class="line">    - minute: <span class="string">'*'</span></div><div class="line">    - hour: 20</div><div class="line">    - daymonth: <span class="string">'*'</span></div><div class="line">    - month: <span class="string">'*'</span></div><div class="line">    - dayweek: <span class="string">'*'</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/49.png?raw=true" alt="49"><figcaption class="figure__caption">49</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，*需要用单引号引起来。当然我们还可以使用file.managed模块来管理cron，因为系统的cron都是以配置文件的形式存在的。想要删除该cron，需要增加：<br>cron.absent:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;- name: /bin/touch /tmp/111.txt</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两者不能共存，要想删除一个cron，那之前的present就得去掉。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 top.sls</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># vim /srv/salt/top.sls</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/50.png?raw=true" alt="50"><figcaption class="figure__caption">50</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端刷新</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' state.highstate</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/51.png?raw=true" alt="51"><figcaption class="figure__caption">51</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/52.png?raw=true" alt="52"><figcaption class="figure__caption">52</figcaption></figure></p>
<h2 id="9-saltstack-几个常用的命令"><a href="#9-saltstack-几个常用的命令" class="headerlink" title="9.saltstack 几个常用的命令"></a>9.saltstack 几个常用的命令</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cp.get_file  拷贝master上的文件到客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' cp.get_file salt://apache.sls /tmp/yanyi.com</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/53.png?raw=true" alt="53"><figcaption class="figure__caption">53</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cp.get_dir 拷贝目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@web9 ~]<span class="comment"># salt 'web10.yanyi.com' cp.get_dir salt://test /tmp/yanyi</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/54.png?raw=true" alt="54"><figcaption class="figure__caption">54</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;会自动在客户端创建test目录，所以后面不要加test，如果写成 /tmp/yanyi/  则会在/tmp/yanyi/目录下又创建test</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt-run manage.up  显示存活的minion</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/55.png?raw=true" alt="55"><figcaption class="figure__caption">55</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;salt ‘*’ cmd.script salt://test/1.sh  命令行下执行master上的shell脚本</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Saltstack%20%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/56.png?raw=true" alt="56"><figcaption class="figure__caption">56</figcaption></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Resin/1. resin安装与配置/">resin安装与配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Resin/">Resin</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;resin 同样也需要 jdk 的支持，所以第一步也是安装 jdk 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://caucho.com/" target="_blank" rel="external">resin官方网站</a> ，它分两个版本，resin 是开源的，另外一个 resinpro 为商业版本。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># wget http://caucho.com/download/resin-4.0.51.tar.gz</span></div><div class="line">[root@192 src]<span class="comment"># tar zxvf resin-4.0.51.tar.gz </span></div><div class="line">[root@192 src]<span class="comment"># cd resin-4.0.51</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/resin --with-java-home=/usr/<span class="built_in">local</span>/jdk1.8.0_111</div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 resin-4.0.51]<span class="comment"># /etc/init.d/resin start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面简单配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 resin-4.0.51]<span class="comment"># cd /usr/local/resin/conf/</span></div><div class="line">[root@192 conf]<span class="comment"># vim resin.xml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;resin 的配置文件和 tomcat 的很像，基本结构为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;cluster id=<span class="string">"app"</span>&gt;</div><div class="line">&lt;host&gt;&lt;/host&gt;</div><div class="line">&lt;/cluster&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中虚拟主机配置就在 <host></host> 里配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># vim resin.xml</span></div><div class="line"></div><div class="line">&lt;host id=<span class="string">"www.123.com"</span> root-directory=<span class="string">"."</span>&gt;</div><div class="line">  &lt;web-app id=<span class="string">"/"</span> root-directory=<span class="string">"/tmp/resin"</span>&gt;</div><div class="line">&lt;/host&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，这里要放在  <cluster id="app"> 下边</cluster></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建文件夹，并重启 resin</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># mkdir /tmp/resin</span></div><div class="line">[root@192 conf]<span class="comment"># service resin stop</span></div><div class="line">Stopping resin: .</div><div class="line">[root@192 conf]<span class="comment"># service resin start</span></div><div class="line">Starting resin: .</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑一个测试文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># vim /tmp/resin/222.jsp</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;html&gt;&lt;body&gt;&lt;center&gt; </div><div class="line"> </div><div class="line">Now time is: &lt;%=new java.util.Date()%&gt; </div><div class="line"> </div><div class="line">&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># curl -x127.0.0.1:80 www.123.com/222.jsp</span></div><div class="line">&lt;html&gt;&lt;body&gt;&lt;center&gt; </div><div class="line"> </div><div class="line">Now time is: Wed Apr 05 13:30:44 CST 2017 </div><div class="line"> </div><div class="line">&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改监听端口，需编辑配置文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># vim resin.properties</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/resin%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/resin%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 conf]<span class="comment"># service resin stop</span></div><div class="line">Stopping resin: .</div><div class="line">[root@192 conf]<span class="comment"># service resin start</span></div><div class="line">Starting resin: .</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;登录本地 IP 就能访问 resin 的默认页面</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/resin%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/03.png?raw=true" alt=""></figure></p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/16">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
